from django.http import HttpResponse
from django.shortcuts import render

# Create your views here.
from django.views import View
from libs.captcha.captcha import captcha
from django_redis import get_redis_connection


class  ImageCodeView(View):
    def get(self,request,uuid):
        #1.接收路由中的uuid
        #2.生成图片验证码和图片二进制
        #text是码字符串，image是图片
        text,image = captcha.generate_captcha()
        #3.通过redis把图片存起来
        redis_cli = get_redis_connection("code")
        #name, time, value
        redis_cli.setex(uuid,100,text)
        #4.返回二进制图片
        #content_type = 响应体数据类型 语法形式：大类/小类 (MINI类型)
        #图片：image/jpeg,image/gif,image/png
        return HttpResponse(image,content_type='image/jpeg')